package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import model.Aluguel;
import model.Hospede;
import model.Quarto;

import control.CtrlAluguel;
import control.CtrlHospede;
import control.CtrlQuarto;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class JFrameBuscaAluguelQuarto extends JFrame {

	private JPanel contentPane;
	private JTable tblAluguel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameBuscaAluguelQuarto frame = new JFrameBuscaAluguelQuarto();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameBuscaAluguelQuarto() {
		setTitle("Busca");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 420, 476);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setResizable(false);
		
		JLabel lblQuarto = new JLabel("Quarto:");
		lblQuarto.setBounds(10, 11, 89, 14);
		contentPane.add(lblQuarto);
		
		final JComboBox cboQuarto = new JComboBox();
		cboQuarto.setBounds(109, 8, 196, 20);
		contentPane.add(cboQuarto);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 36, 394, 396);
		contentPane.add(scrollPane);
		
		CtrlQuarto ctrlHos = new CtrlQuarto();
		List<Quarto> listaQuarto = new ArrayList<Quarto>();
		listaQuarto = ctrlHos.listaCompleta();
		final List<Quarto> listaQuartoAux = ctrlHos.listaCompleta();
		
		for(Quarto quarto: listaQuarto){
			cboQuarto.addItem("Andar: "+quarto.getAndar()+" Quarto:"+quarto.getNumeroQuarto());	
		}
		
		tblAluguel = new JTable();
		tblAluguel.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"H\u00F3spede", "Data Entrada", "Data Saida"
			}
		));
		
		
		scrollPane.setViewportView(tblAluguel);
		final DefaultTableModel model = (DefaultTableModel) tblAluguel.getModel();
		
		
		
		JButton btnBuscar = new JButton("Buscar");
		
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(model.getRowCount()>0){
					model.setRowCount(0);
				}
				Quarto busca = listaQuartoAux.get(cboQuarto.getSelectedIndex());
				List<Aluguel> alugueis = new ArrayList<>();
				CtrlAluguel ctrlAluguel = new CtrlAluguel();
				alugueis = ctrlAluguel.listaAlugueisQuarto(busca);
				SimpleDateFormat formatBra = new SimpleDateFormat("dd/MM/yyyy");
				
				for(Aluguel aux: alugueis){
					Object[] objetoAluguel = new Object[3];
					objetoAluguel[0] = (""+aux.getHospede().getNome());
					objetoAluguel[1] = (""+formatBra.format(aux.getDateInicial()));
					objetoAluguel[2] = (""+formatBra.format(aux.getDataFinal()));
					model.addRow(objetoAluguel);
				}
				
			}
		});
		btnBuscar.setBounds(315, 7, 89, 23);
		contentPane.add(btnBuscar);
	}
}
